package com.shd.edanpei.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.alibaba.fastjson.JSONObject;
import com.shd.edanpei.common.Constant;
import com.shd.edanpei.ui.activity.DynamicsDetailsActivity;
import com.shd.edanpei.ui.activity.LoadingActivity;
import com.shd.edanpei.ui.activity.SplashActivity;

import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.service.JPushMessageReceiver;


public class JpushReceiver extends BroadcastReceiver {
    public String TAG="JpushReceiver.class";
    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        String content = bundle.getString(JPushInterface.EXTRA_ALERT);
        String title = bundle.getString(JPushInterface.EXTRA_TITLE);
        String message = bundle.getString(JPushInterface.EXTRA_MESSAGE);
        String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
        String file = bundle.getString(JPushInterface.EXTRA_MSG_ID);
        //{"articleId":"121","from":"E单配","type":"3"}
        if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
            //Logger.d(TAG, "JPush 用户注册成功");

        } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
            //Logger.d(TAG, "接受到推送下来的自定义消息");

        } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
            //Logger.d(TAG, "接受到推送下来的通知");

            //receivingNotification(context,bundle);

        } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
           // Logger.d(TAG, "用户点击打开了通知");
            if(null!=extras){
                JSONObject jsonObject = JSONObject.parseObject(extras);
                //System.out.println(jsonObject.getString("articleId"));
                //System.out.println(jsonObject.getString("from"));
                //System.out.println(jsonObject.getInteger("type"));
                if(extras.contains("type")&&3==jsonObject.getInteger("type")){
                    Intent intent1=new Intent();
                    intent1.putExtra(Constant.MESSAGE_ID,jsonObject.getString("articleId"));
                    intent1.putExtra(Constant.MESSAGE_TYPE,jsonObject.getInteger("type"));
                    intent1.setClass(context, DynamicsDetailsActivity.class);
                    context.startActivity(intent1);
                }else{
                    context.startActivity(new Intent(context,SplashActivity.class));
                }
            }

        }


    }


}
